package com.googlecode.worktools.ui;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.worktools.service.datatruncation.DBConn;
import com.googlecode.worktools.utils.UIUtils;




/**
 * 数据截断分析功能面板
 * @author Kobe Su,2009-11-18
 */
public class DataTruncationPanel extends BaseFunctionPanel {
	private static final Log log = LogFactory.getLog(DataTruncationPanel.class);
	
	private JPanel sqlPanel;
	private JPanel dbPanel;
	private JButton analysisButton;
	private JButton dbButton;
	private JPanel jPanel1;
	private JTextArea resultAreal;
	private JLabel resultLabel;
	private JTextArea sqlArea1;
	private JLabel sqlLabel;
	private JPanel resultPanel;
	private JTextField passwordText;
	private JLabel password;
	private JTextField usrnameText;
	private JLabel username;
	private JTextField dburlText;
	private JLabel dburl;
    private JScrollPane sqlScrollPane;
    private JScrollPane resultScrollPane;
    
	private Connection conn = null;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new DataTruncationPanel());
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public DataTruncationPanel() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {

			{
				setLayout(new BorderLayout());
				sqlPanel = new JPanel();
				BorderLayout sqlPanelLayout = new BorderLayout();
				sqlPanel.setLayout(sqlPanelLayout);
				add(sqlPanel, BorderLayout.CENTER);
				sqlPanel.setPreferredSize(new java.awt.Dimension(565, 141));
				sqlPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
				{
					sqlLabel = new JLabel();
					sqlPanel.add(sqlLabel, BorderLayout.NORTH);
					sqlLabel.setText("Insert/Update SQL\uff1a");
				}
				{
					sqlArea1 = new JTextArea();
					sqlArea1.setText("Supported SQL Pattern\uff1a\nINSERT INTO table_name (column1,column2,...) VALUES (value1,value2,...)\nOR\nUPDATE table_name SET column1=value1,column2=value2,... WHERE <search_condition>");
					sqlArea1.setLineWrap(true);
					sqlArea1.setPreferredSize(new java.awt.Dimension(548, 180));
					//sqlArea1.setBorder(new LineBorder(new java.awt.Color(0,0,0), 2, false));
					sqlScrollPane = new JScrollPane(sqlArea1);
					sqlScrollPane.setPreferredSize(new Dimension(548, 100));
					sqlScrollPane.setBorder(new LineBorder(new java.awt.Color(0,0,0), 2, false));
					sqlPanel.add(sqlScrollPane, BorderLayout.CENTER);
				}
				{
					analysisButton = new JButton();
					sqlPanel.add(analysisButton, BorderLayout.SOUTH);
					analysisButton.setText("Analyze");
					analysisButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							analyze();
						}
					});
				}
			}
			{
				resultPanel = new JPanel();
				BorderLayout resultPanelLayout = new BorderLayout();
				resultPanel.setLayout(resultPanelLayout);
				add(resultPanel, BorderLayout.SOUTH);
				resultPanel.setPreferredSize(new java.awt.Dimension(565, 151));
				resultPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
				{
					resultLabel = new JLabel();
					resultPanel.add(resultLabel, BorderLayout.NORTH);
					resultLabel.setText("Report\uff1a");
				}
				{
					resultAreal = new JTextArea();
					resultAreal.setLineWrap(true);
					resultAreal.setPreferredSize(new java.awt.Dimension(565, 133));
					//resultAreal.setBorder(new LineBorder(new java.awt.Color(0,0,0), 2, false));
					resultScrollPane = new JScrollPane(resultAreal);
					resultScrollPane.setPreferredSize(new Dimension(565, 120));
					resultScrollPane.setBorder(new LineBorder(new java.awt.Color(0,0,0), 2, false));
					resultPanel.add(resultScrollPane, BorderLayout.CENTER);
				}
			}
			{
				jPanel1 = new JPanel();
				BorderLayout jPanel1Layout = new BorderLayout();
				jPanel1.setLayout(jPanel1Layout);
				add(jPanel1, BorderLayout.NORTH);
				jPanel1.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 10));
				{
					dbPanel = new JPanel();
					jPanel1.add(dbPanel, BorderLayout.CENTER);
					GridBagLayout dbPanelLayout = new GridBagLayout();
					dbPanelLayout.rowWeights = new double[] {0.1, 0.1, 0.1};
					dbPanelLayout.rowHeights = new int[] {7, 7, 7};
					dbPanelLayout.columnWeights = new double[] {0.0, 0.0, 0.0};
					dbPanelLayout.columnWidths = new int[] {28, 322, 83};
					dbPanel.setLayout(dbPanelLayout);
					dbPanel.setPreferredSize(new java.awt.Dimension(548, 113));
					TitledBorder titled = BorderFactory.createTitledBorder(new LineBorder(new java.awt.Color(0,0,0), 2, false),"DB Connection");
					titled.setTitleColor(new Color(255, 0, 0));
					dbPanel.setBorder(titled);
					{
						dburl = new JLabel();
						dbPanel.add(dburl, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						dburl.setText("DB URL\uff1a");
						dburl.setPreferredSize(new java.awt.Dimension(167, 18));
					}
					{
						dburlText = new JTextField();
						dbPanel.add(dburlText, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
						dburlText.setText("jdbc:jtds:sqlserver://192.168.0.207:1433/cqeis_import");
						dburlText.setPreferredSize(new java.awt.Dimension(233, 22));
					}
					{
						username = new JLabel();
						dbPanel.add(username, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						username.setText("User Name\uff1a");
						username.setPreferredSize(new java.awt.Dimension(266, 18));
					}
					{
						usrnameText = new JTextField();
						dbPanel.add(usrnameText, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 0, 0, 0), 0, 0));
						usrnameText.setText("webuser");
						usrnameText.setPreferredSize(new java.awt.Dimension(104, 22));
					}
					{
						password = new JLabel();
						dbPanel.add(password, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
						password.setText("Password\uff1a");
					}
					{
						passwordText = new JTextField();
						dbPanel.add(passwordText, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 0, 1, 0), 0, 0));
						passwordText.setText("zdsoft");
						passwordText.setPreferredSize(new java.awt.Dimension(322, 25));
					}
					{
						dbButton = new JButton();
						dbPanel.add(dbButton, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(1, 0, 0, 0), 0, 0));
						dbButton.setText("Connect");
						dbButton.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								String url = dburlText.getText();
								String userName = usrnameText.getText();
								String password = passwordText.getText();
								DBConn.getInstance().setUrl(url);
								DBConn.getInstance().setUserName(userName);
								DBConn.getInstance().setPassword(password);
								try {
									conn = DBConn.getInstance().getConn();
									UIUtils.showSuccessMsg(dbButton,"数据库连接成功！");
								} catch (ClassNotFoundException e) {
									UIUtils.showErrorMsg(dbButton,"数据库连接失败："+e.getMessage());
									log.error(e.getMessage(), e);
								} catch (SQLException e) {
									UIUtils.showErrorMsg(dbButton,"数据库连接失败："+e.getMessage());
									log.error(e.getMessage(), e);
								}
							}
						});
					}
				}
			}

			this.setSize(680, 527);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	
	private void analyze() {
		if(conn == null) {
			UIUtils.showErrorMsg(analysisButton, "请先连接数据库！");
			return;
		}
		String sql = sqlArea1.getText();
		String result = DBConn.getInstance().analyze(sql);
		resultAreal.setText(result);
	}

}
